package com.chenyue.cm.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.user.dao.JdThirdApplyMapper;
import com.chenyue.cm.user.dao.JdThirdInformationMapper;
import com.chenyue.cm.user.domain.JdThirdApply;
import com.chenyue.cm.user.domain.JdThirdInformation;
import com.chenyue.cm.user.service.JdThirdApplyService;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;


/**
 * @author chenyue-cm
 */
@Service(value = "JdThirdApplyService")
public class JdThirdApplyServiceImpl extends BaseServiceImpl<JdThirdApply> implements JdThirdApplyService {

    private final JdThirdApplyMapper jdThirdApplyMapper;

    private final JdThirdInformationMapper jdThirdInformationMapper;

    public JdThirdApplyServiceImpl(@Qualifier("jdThirdApplyMapper") JdThirdApplyMapper jdThirdApplyMapper,
                                   @Qualifier("jdThirdInformationMapper") JdThirdInformationMapper jdThirdInformationMapper1) {
        this.jdThirdApplyMapper = jdThirdApplyMapper;
        this.jdThirdInformationMapper = jdThirdInformationMapper1;
    }

    @Override
    @Resource(name = "jdThirdApplyMapper")
    public void setBaseMapper(LcBaseMapper<JdThirdApply, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public Success<String> thirdApply(Integer userId, Integer type) {
        JdThirdApply thirdApply = lambdaQuery()
                .ne(JdThirdApply::getCheckState, 2)
                .eq(JdThirdApply::getUserId, userId)
                .last("limit 1")
                .one();
        if (null != thirdApply) {
            return Success.error("该企业已入驻");
        }
        JdThirdApply newThird = new JdThirdApply();
        newThird.setUserId(userId);
        newThird.setCheckState(0);
        newThird.setApplyType(type);
        newThird.setCt(DateUtils.getToday());
        jdThirdApplyMapper.insert(newThird);
        return Success.success("入驻申请已提交", "");
    }

    @Override
    public JdThirdInformation thirdInformation(Integer userId) {
        LambdaQueryWrapper<JdThirdInformation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdThirdInformation::getUserId, userId);
        wrapper.last(" limit 1 ");
        return jdThirdInformationMapper.selectOne(wrapper);
    }

    /**
     * 编辑三方信息
     *
     * @param information 三方信息
     */
    @Override
    public void editThirdInfo(JdThirdInformation information) {
        if (information.getId() == null || information.getId() == 0) {
            jdThirdInformationMapper.insert(information);
        } else {
            jdThirdInformationMapper.updateById(information);
        }
    }
}
